<?php
namespace app\index\logic;

use think\Db;

class Article extends Base
{
    // 获取时间的列表
    public function timeTable($pm)
    {
        if (!empty($pm['keyword'])) {
            $where['title'] = ['like', "%{$pm['keyword']}%"];
        } else {
            $where = [];
        }
        $list = Db::name('article')->where($where)->order($pm['value'])->page($pm['curr'], $pm['limit'])->select();
        foreach ($list as &$item) {
            $item['tags'] = $this->logic()->tag()->getTagsByArticleId($item['id']);
        }
        $count = Db::name('article')->where($where)->count();
        return ['count'=>$count, 'list'=>$list];
    }

    // 获取某个标签的列表
    public function tagTable($pm)
    {
        $where['id'] = ['in', implode(',', $this->logic()->tag()->getArticleIdsByTagId($pm['value']))];
        if (!empty($pm['keyword'])) {
            $where['title'] = ['like', "%{$pm['keyword']}%"];
        }
        $list = Db::name('article')->where($where)->order('id desc')->page($pm['curr'], $pm['limit'])->select();
        $count = Db::name('article')->where($where)->count();
        return ['count'=>$count, 'list'=>$list];
    }

    // 获取月份的列表
    public function monthTable($pm)
    {
        $min_time = strtotime($pm['value']);
        list($year, $month) = explode('-',$pm['value']);
        if ($month < 12) {
            $month++;
        } else {
            $month = 01;
            $year++;
        }
        $max_time = strtotime($year.'-'.$month);
        $where[] = ['create_time' ,'>=', $min_time];
        $where[] = ['create_time' ,'<=', $max_time];

        if (!empty($pm['keyword'])) {
            $where['title'] = ['like', "%{$pm['keyword']}%"];
        }
        $list = Db::name('article')->where($where)->order('id desc')->page($pm['curr'], $pm['limit'])->select();
        $count = Db::name('article')->where($where)->count();
        return ['count'=>$count, 'list'=>$list];
    }

    // 获取文章分类的列表
    public function categoryTable($pm)
    {
        $list = $this->logic()->articleSort()->getAllSubIds($pm['value'], true);
        $ids = array_column($list, 'id');
        $where['sort_id'] = ['in', implode(',', $ids)];
        if (!empty($pm['keyword'])) {
            $where['title'] = ['like', "%{$pm['keyword']}%"];
        }
        $list = Db::name('article')->where($where)->order('id desc')->page($pm['curr'], $pm['limit'])->select();
        $count = Db::name('article')->where($where)->count();
        return ['count'=>$count, 'list'=>$list];
    }
}